package com.blogsoho.taurus.controller;

import com.blogsoho.taurus.constants.URIMapping;
import com.blogsoho.taurus.vm.ResponseWrapper;
import com.blogsoho.taurus.vo.ContentVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by user on 2017/7/10.
 */
@Controller
@RequestMapping(URIMapping.BLOG)
public class ArchiveController extends BaseController {

    @GetMapping({"archive/{id:\\d+}", "archive"})
    public String archive(ModelMap model, @PathVariable(required = false) Integer id) throws Exception {
        model.addAttribute("category", categoryService.getAllCategory());
        model.addAttribute("categoryName", id == null ? "Category" : categoryService.getCategoryName(id));
        ResponseWrapper<List<ContentVO>> responseWrapper = contentService.getContentByCategoryId(id == null ? 0 : id);
        if (responseWrapper.isSuccess()) {
            model.addAttribute("contents", responseWrapper.getData());
        }
        return "archive";
    }
}
